package com.jcl.recruit.timer.crawler.service.impl;

import com.google.gson.Gson;
import com.jcl.recruit.resume.model.ResumeOrig;
import com.jcl.recruit.searchresume.model.SearchResume;
import com.jcl.recruit.searchresume.model.SearchResumeEdu;
import com.jcl.recruit.searchresume.model.SearchResumeWork;
import com.jcl.recruit.timer.crawler.bean.JxBean;
import com.jcl.recruit.timer.crawler.bean.JxResumeBean;
import com.jcl.recruit.timer.crawler.bean.ZhaopinJsonBean;
import com.jcl.recruit.timer.crawler.service.CrawlerChannelResumeJx;
import org.apache.commons.lang3.StringUtils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName crawlerChannelResumeTaskService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/30 15:57
 * @Version 1.0
 **/
public class ZhaopinCrawlerResumeTask implements CrawlerChannelResumeJx {

    @Override
    public JxBean crawlerResume(List<ResumeOrig> resumeOrigList) throws Exception{
        JxBean jxBean = new JxBean();
        List<JxResumeBean> jxResumeBeans = new ArrayList<>();
        jxBean.setJxResumeBeans(jxResumeBeans);

        for(ResumeOrig orig: resumeOrigList) {

            jxBean.setExternalId(orig.getId());
            JxResumeBean jxResumeBean = new JxResumeBean();
            jxResumeBeans.add(jxResumeBean);

            String json = orig.getContent();
            ZhaopinJsonBean zhaopinJsonBean = new Gson().fromJson(json, ZhaopinJsonBean.class);
            SearchResume searchResume = new SearchResume();
            searchResume.setCreatetime(new Date());
            searchResume.setUpdateDate(searchResume.getCreatetime());
            searchResume.setRealname(zhaopinJsonBean.getUserName());
            searchResume.setSex(zhaopinJsonBean.getGender());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            if (zhaopinJsonBean.getAge() != null) {
                Integer birthYear = Integer.valueOf(sdf.format(new Date())) - zhaopinJsonBean.getAge();
                searchResume.setBirthday(sdf.parse(birthYear.toString(), new ParsePosition(0)));
            }
            searchResume.setNativePlace(zhaopinJsonBean.getDesireCity());
            searchResume.setCurrentCity(zhaopinJsonBean.getDesireCity());
            searchResume.setCurrentAddress(zhaopinJsonBean.getDesireCity());
            searchResume.setCurrentSalary(zhaopinJsonBean.getLastJobDetail().getSalary());
            if (StringUtils.isNotBlank(zhaopinJsonBean.getDesiredSalary()) && !zhaopinJsonBean.getDesiredSalary().equals("面议")) {
                String desireSalarys = zhaopinJsonBean.getDesiredSalary();
                if (desireSalarys.contains("-")) {
                    searchResume.setDesireSalaryStart(Integer.valueOf(desireSalarys.substring(0, desireSalarys.indexOf("-"))));
                    searchResume.setDesireSalaryEnd(Integer.valueOf(desireSalarys.substring(desireSalarys.indexOf("-") + 1, desireSalarys.indexOf("元"))));
                } else {
                    searchResume.setDesireSalaryStart(Integer.valueOf(desireSalarys.substring(0, desireSalarys.indexOf("元"))));
                }
            }
            searchResume.setWorkyear(zhaopinJsonBean.getWorkyears());
            searchResume.setWorkLastPosition(zhaopinJsonBean.getLastJobDetail().getJobName());
            searchResume.setWorkLastCompany(zhaopinJsonBean.getLastJobDetail().getCompanyName());
            searchResume.setWorkDetail(zhaopinJsonBean.getLastJobDetail().getDescription());
            searchResume.setEduLastEducation(zhaopinJsonBean.getEduLevel());
            searchResume.setEduLastSchool(zhaopinJsonBean.getSchoolDetail().getSchoolName());
            searchResume.setEduLastMajor(zhaopinJsonBean.getSchoolDetail().getMajor());
            searchResume.setSourceSub("zhaopin");
            searchResume.setStatus(0);
            searchResume.setArrivalInfo(zhaopinJsonBean.getCareerStatus());
            searchResume.setApplyStatus(0);
            searchResume.setKeyword(orig.getKeyword());

            jxResumeBean.setSearchResume(searchResume);

            List<SearchResumeWork> works = new ArrayList<>();
            SearchResumeWork searchResumeWork = new SearchResumeWork();
            searchResumeWork.setStartDate(zhaopinJsonBean.getLastJobDetail().getBeginDate());
            searchResumeWork.setEndDate(zhaopinJsonBean.getLastJobDetail().getEndDate());
            searchResumeWork.setCompany(zhaopinJsonBean.getLastJobDetail().getCompanyName());
            searchResumeWork.setDuty(zhaopinJsonBean.getLastJobDetail().getJobName());
            searchResumeWork.setPosition(zhaopinJsonBean.getLastJobDetail().getDescription());
            works.add(searchResumeWork);

            List<SearchResumeEdu> edus = new ArrayList<>();
            SearchResumeEdu searchResumeEdu = new SearchResumeEdu();
            searchResumeEdu.setStartDate(zhaopinJsonBean.getSchoolDetail().getBeginDate());
            searchResumeEdu.setEndDate(zhaopinJsonBean.getSchoolDetail().getEndDate());
            searchResumeEdu.setSchool(zhaopinJsonBean.getSchoolDetail().getSchoolName());
            searchResumeEdu.setMajor(zhaopinJsonBean.getSchoolDetail().getMajor());
            searchResumeEdu.setDegree(zhaopinJsonBean.getEduLevel());
            edus.add(searchResumeEdu);

            jxResumeBean.setSearchResumeEdus(edus);
            jxResumeBean.setResumeWorks(works);
        }

        return jxBean;
    }
}
